﻿#include "algworker.h"
#include <string>
#include <QDebug>


AlgWorker::AlgWorker(QObject *parent) : QObject(parent)
{
    for (int i = 0; i < 20; i++)
    {
        m_nNGorOK1[i] = 0;
        m_nNGorOK2[i] = 0;
        m_nNGorOK3[i] = 0;
    }
    m_part.parameter1 = 15;
    m_part.parameter2 = 20;
    m_part.parameter3 = 20;
    m_part.parameter4 = 30;

    HalconCpp::HObject imgA;
    HalconCpp::HObject imgB;
    for (int i = 0; i < 3; i++)
    {
        lImgAs << imgA;
        lImgBs << imgB;
    }
}

AlgWorker::~AlgWorker()
{

}



void AlgWorker::Initialize()
{

}

void AlgWorker::start(CameraData m_cImageInfo)
{
    QTime time;
    time.start();

    int result = 0;
    int ccdIndex = m_cImageInfo.m_nIndex;

    if(m_nImplement ==1){
        // 开启检测
        switch (ccdIndex) {
        case 1:
            //CCD1
            lImgAs[0] = lImgBs[0];
            lImgBs[0] = m_cImageInfo.Hobj;
            if(m_nIndex > 0){
                result = CCD_Alg(lImgAs[0],lImgBs[0], m_cImageInfo.m_nIndex);
                m_cImageInfo.Hobj = m_algImpurity.g_ho_ImageResult;
                if(result !=0){
                    m_nNGorOK1[m_nCountD1] = result;
                    m_nSResult = result;
                }
            }
            break;
        case 2:
            lImgAs[1] = lImgBs[1];
            lImgBs[1] = m_cImageInfo.Hobj;
            if(m_nIndex > 0  && m_bD2){
                result = CCD_Alg(lImgAs[1],lImgBs[1], m_cImageInfo.m_nIndex);
                m_cImageInfo.Hobj = m_algImpurity.g_ho_ImageResult;
                if(result !=0){
                    m_nNGorOK2[m_nCountD2] = result;
                    m_nSResult = result;
                }
            }
            break;
        case 3:
            lImgAs[2] = lImgBs[2];
            lImgBs[2] = m_cImageInfo.Hobj;
            if(m_nIndex > 0  && m_bD3){
                result = CCD_Alg(lImgAs[2],lImgBs[2], m_cImageInfo.m_nIndex);
                m_cImageInfo.Hobj = m_algImpurity.g_ho_ImageResult;
                if(result !=0){
                    m_nNGorOK3[m_nCountD3] = result;
                    m_nSResult = result;
                }
            }
            break;
        }


        m_nIndex++;
        if(m_nIndex > 19){
            m_nOResult = m_nSResult;
            m_nSResult = 0;
            m_nIndex = 0;
            if(m_nCount < 28){
                emit sigCameraResult(m_nCount);
            }
			m_nCountD1++;
			m_nCount++;
			if (m_bD2) {
				m_nCountD2++;
			}

			if (m_bD3) {
				m_nCountD3++;
			}
			if (m_nCountD1 > 3) {
				m_bD2 = true;
			}
			if (m_nCountD1 > 7) {
				m_bD3 = true;
			}

            if(m_nCountD1 > 19){
                m_nCountD1 = 0;
            }

            if(m_nCountD2 > 19){
                m_nCountD2 = 0;
            }

            if(m_nCountD3 > 19){
                clearAllData();
            }

            if(m_nCount > 27){
                m_nCount = 0;
            }
        }
    }

    hvCCDHandle = m_cImageInfo.hv_WindowHandle;
    msg = QString("Time:%1s").arg(time.elapsed() / 1000.0);
    show(m_cImageInfo.Hobj, hvCCDHandle, m_nOResult);
    m_cImageInfo.m_nNGorOK = result;
    if (m_nSave){
        emit sigSaveImgStart(m_cImageInfo);
    }

}

void AlgWorker::show(HalconCpp::HObject img, HalconCpp::HTuple hv_WindowHandle, int result)
{

    HalconCpp::HTuple w, h;
    HalconCpp::GetImageSize(img, &w, &h);
    HalconCpp::SetPart(hv_WindowHandle, 0, 0, h, w);
    HalconCpp::DispObj(img, hv_WindowHandle);
    HObject Rect, Contour;


    try
    {
        HalconCpp::GenRectangle1(&Rect, m_algPlace.x1, m_algPlace.y1, m_algPlace.x2, m_algPlace.y2);
        HalconCpp::GenContourRegionXld(Rect, &Contour, "border");
        HalconCpp::SetColor(hv_WindowHandle, "red");
        HalconCpp::DispObj(Contour, hv_WindowHandle);

        SetTposition(hv_WindowHandle, HTuple(30), HTuple(50));
        if (result != 0)
        {
            SetColor(hv_WindowHandle, "red");
            WriteString(hv_WindowHandle, "NG");
        }
        else
        {
            SetColor(hv_WindowHandle, "green");
            WriteString(hv_WindowHandle, "OK");
        }
        SetTposition(hv_WindowHandle, HTuple(90), HTuple(50));
        WriteString(hv_WindowHandle, msg.toStdString().c_str());
    }
    catch (HException ex)
    {
        qDebug() << "检测区域设置 错误";
    }

}


int AlgWorker::CCD_Alg(HObject imgA, HObject imgB, int ccdIndex)
{
    ///前面一个Param 是图像采集的张数  后面一个Param 是相机的序号
    m_algImpurity.m_part = m_part;
    int result = m_algImpurity.detect(imgA,imgB, m_nIndex, ccdIndex);
    return result;
}

void AlgWorker::clearAllData()
{
    m_nCount = 0;
    m_nIndex = 0;
    m_nCountD1 = 0;
    m_nCountD2 = 0;
    m_nCountD3 = 0;
    for(int i=0;i<20;i++){
        m_nNGorOK1[i] = 0;
        m_nNGorOK2[i] = 0;
        m_nNGorOK3[i] = 0;
    }
    m_bD2 = false;
    m_bD3 = false;
}
